import { Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, JoinColumn } from "typeorm";
import { EntityModel } from "@midwayjs/orm";
import { Users } from "./Users.entity";
import { RescueRecord } from "./RescueRecord.entity";
import { Child } from "./Child.entity";

@EntityModel("rescue_info", { schema: "heart_neighbor" })
export class RescueInfo {
  @PrimaryGeneratedColumn({ type: "int", name: "id", comment: "Primary Key" })
  id!: number;

  @Column("datetime", {
    name: "create_time",
    nullable: true,
    comment: "Create Time",
    default: () => "CURRENT_TIMESTAMP",
  })
  createTime!: Date | null;


  @Column("datetime", {
    name: "end_time",
    nullable: true,
    comment: "Create Time",
    default: () => "CURRENT_TIMESTAMP",
  })
  endTime!: Date | null;
  
  @Column("varchar", {
    name: "name",
    nullable: true,
    comment: "救助名称",
    length: 255,
  })
  name!: string | null;

  @Column("int", { name: "child_id", nullable: true, comment: "儿童ID" })
  childId!: number | null;

  @Column("int", { name: "guardian_id", nullable: true, comment: "发起用户" })
  guardianId!: number | null;

  @Column("tinyint", {
    name: "rescue_status",
    nullable: true,
    comment: "状态，1=待处理，2=已处理",
    width: 1,
    default: () => "'1'",
  })
  rescueStatus!: number | null;

  @Column("varchar", {
    name: "rescue_content",
    nullable: true,
    comment: "救助内容",
    length: 255,
  })
  rescueContent!: string | null;

  @Column("varchar", {
    name: "rescue_img",
    nullable: true,
    comment: "救助图片",
    length: 255,
  })
  rescueImg!: string | null;

  @Column("varchar", {
    name: "address",
    nullable: true,
    comment: "救助地址",
    length: 500,
  })
  address!: string | null;

  @Column("decimal", {
    name: "lat",
    nullable: true,
    comment: "纬度",
    precision: 10,
    scale: 6,
  })
  lat!: number | null;

  @Column("decimal", {
    name: "lng",
    nullable: true,
    comment: "经度",
    precision: 10,
    scale: 6,
  })
  lng!: number | null;

  // 多对一关系：多个救助信息由一个用户发起
  @ManyToOne(() => Users, (user) => user.rescueInfos, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "guardian_id" })
  guardian!: Users;

  // 多对一关系：多个救助信息关联一个儿童
  @ManyToOne(() => Child, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "child_id" })
  child!: Child;

  // 一对多关系：一个救助信息可以有多个救助记录
  @OneToMany(() => RescueRecord, (record) => record.rescueInfo, { createForeignKeyConstraints: false })
  rescueRecords!: RescueRecord[];
}
